#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020-05-22 23:26
# @Author  : Sunnn
# @File    : LogTools.py
# @Software: PyCharm
# @intro   :
import traceback
import os
import sys
import threading
import time

import boto3
from boto3.s3.transfer import TransferConfig
from boto3.session import Session


class ProgressPercentage(object):
    def __init__(self, filename):
        self._filename = filename
        self._size = float(os.path.getsize(filename))
        self._seen_so_far = 0
        self._lock = threading.Lock()
        self._start_time = int(time.time())

    def __call__(self, bytes_amount):
        # To simplify we'll assume this is hooked up
        # to a single filename.
        with self._lock:
            self._seen_so_far += bytes_amount
            percentage = round((self._seen_so_far / self._size) * 100, 2)
            if (int(time.time()) - self._start_time) % 60 == 0:
                print('{} is the file name. {} out of {} done. The percentage completed is {} %'.format(
                    str(self._filename), str(self._seen_so_far), str(self._size), str(percentage)))
            sys.stdout.flush()


def parse_error_object():
    """
        获取异常信息的具体内容
        异常类型、异常内容、异常代码位置
    :return: error info
    """
    return '{}'.format(traceback.format_exc())


def s3_backupfile(aws_access_key_id, aws_secret_access_key, file_path, s3_path, bucket_name='dianshangstorage'):
    session = Session(aws_access_key_id=aws_access_key_id,
                      aws_secret_access_key=aws_secret_access_key,
                      region_name='cn-northwest-1')
    GB = 1024 ** 3
    s3_client = session.client('s3')
    config = TransferConfig(max_concurrency=50, multipart_threshold=5 * GB)
    s3_client.upload_file(file_path, bucket_name,
                          s3_path, Config=config,
                          Callback=ProgressPercentage(file_path))


if __name__ == '__main__':
    s3_backupfile(aws_access_key_id='X',
                  aws_secret_access_key='X',
                  file_path=r'D:\南哲科技\淘宝全站店铺数据\taobao_tmall_yuanshishuju.zip', bucket_name='dianshangstorage',
                  s3_path='taobao/shop/taobao_tmall_yuanshishuju111.zip12321321')
